package cn.edu.scnu.match.controller;

import cn.edu.scnu.annotation.IgnoreToken;
import cn.edu.scnu.match.service.RegisterService;
import cn.edu.scnu.pojo.RegistrationForm;
import cn.edu.scnu.vo.ResultCode;
import cn.edu.scnu.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
public class RegisterController {
    @Autowired
    private RegisterService registerService;

    /**
     * 用户报名赛事
     *
     * @param form 赛事信息
     * @return 操作响应
     */
    @ApiOperation("用户报名赛事")
    @ApiImplicitParam(name = "form", value = "注册表", dataType = "cn.edu.scnu.pojo.RegistrationForm", paramType = "body", required = true)
    @PostMapping("/registers")
    public SysResult registerMatch(@RequestBody RegistrationForm form) {
        try {
            registerService.registerMatch(form);
            return SysResult.build(ResultCode.CREATED, "报名赛事成功", null);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 用户修改报名表信息
     *
     * @param form 更新后的报名表
     * @return 操作响应码
     */
    @ApiOperation("用户修改报名表信息")
    @ApiImplicitParam(name = "form", value = "注册表", dataType = "cn.edu.scnu.pojo.RegistrationForm", paramType = "body", required = true)
    @PutMapping("/registers")
    public SysResult saveRegisteration(@RequestBody RegistrationForm form) {
        try {
            registerService.saveRegisteration(form);
            return SysResult.build(ResultCode.SUCCESS, "更改报名表信息成功", null);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 通过用户名获取该用户所报名的所有赛事报名表
     * @param userId 用户名
     * @return 用户报名所有赛事报名表的列表
     */
    @IgnoreToken
    @ApiOperation("通过用户ID获取该用户所报名的所有赛事报名表的信息")
    @ApiImplicitParam(name = "userId", value = "用户编号", dataType = "String", paramType = "path", required = true)
    @GetMapping("/registers/{userId}")
    public SysResult getRegisterationsByUsername(@PathVariable("userId") String userId) {
        try {
            List<RegistrationForm> registrationForms = registerService.getRegisterationsByUserId(userId);
            return SysResult.build(ResultCode.SUCCESS, "获取报名表信息成功", registrationForms);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }

    /**
     * 通过报名表编号缴费
     * @param form 需要缴费的报名表
     * @return 缴费结果
     */
    @ApiOperation("通过报名表编号缴费")
    @ApiImplicitParam(name = "entryId", value = "报名表编号", dataType = "String", paramType = "body", required = true)
    @PutMapping("/registers/pay")
    public SysResult payForEntry(@RequestBody RegistrationForm form) {
        try {
            registerService.payForEntry(form.getId());
            return SysResult.build(ResultCode.SUCCESS, "缴费成功", null);
        } catch (Exception e) {
            return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
        }
    }
}
